package com.jxfgyj.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jxfgyj.domain.DO.jxfgyj_user;
import com.jxfgyj.service.UserService;
import com.jxfgyj.util.JsonUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

	private HttpServletResponse http_response;
	private HttpServletRequest http_request;
	/*
	 * 
	 */
	private UserService userService;
	/*
	 * 
	 */
	private jxfgyj_user user;
	private String search;

	/*
	 * 
	 */
	public String UserManage() {
		return "UserManage";
	}

	/*
	 * 
	 */

	public void login() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		jxfgyj_user loginResult = userService.login(user);
		if (loginResult != null) {
			/*
			 * session
			 */
			ActionContext.getContext().getSession().put("loginResult", loginResult);
			pw.write("1");
		} else {
			pw.write("2");
		}
	}

	public void findLogin() throws IOException, IllegalArgumentException, IllegalAccessException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		//
		jxfgyj_user user = (jxfgyj_user) ActionContext.getContext().getSession().get("loginResult");
		if (user != null) {
			pw.write(JsonUtils.toJson(user));
		} else {
			pw.write("2");
		}
	}

	public void logout() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		ActionContext.getContext().getSession().remove("loginResult");
		pw.write("1");
	}

	public void createUser() {
		http_response.setContentType("text/html;charset=utf-8");
		try {
			if (userService.createUser(user)) {

				http_response.getWriter().write("1");

			} else {
				http_response.getWriter().write("-1");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listUser() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		//
		http_response.setContentType("text/html;charset=utf-8");
		List<jxfgyj_user> listUser = userService.listUser();
		try {
			http_response.getWriter().write(gson.toJson(listUser));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void deleteUser() {
		userService.deleteUser(user.getUser_id());

		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 
	 */
	@Override
	public void setServletRequest(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	@Override
	public void setServletResponse(HttpServletResponse http_response) {
		this.http_response = http_response;

	}

	public HttpServletResponse getHttp_response() {
		return http_response;
	}

	public void setHttp_response(HttpServletResponse http_response) {
		this.http_response = http_response;
	}

	public HttpServletRequest getHttp_request() {
		return http_request;
	}

	public void setHttp_request(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	/*
	 * 
	 */
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * 
	 */
	public jxfgyj_user getUser() {
		return user;
	}

	public void setUser(jxfgyj_user user) {
		this.user = user;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	/*
	 * 
	 */

}
